//
//  BaseTableView.swift
//  QFTZ
//
//  Created by apple on 2020/3/30.
//  Copyright © 2020 iOSCoderZhao. All rights reserved.
//

import UIKit
//MARK: - UITableView 基类
class BaseTableView: UITableView {

    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        
        let headerView = UIView.init(frame: CGRect(origin: .zero, size: CGSize(width: 10, height: 0.001)))
        
        // 横屏布局，产品不考虑安全区域，所以设置false
        self.insetsContentViewsToSafeArea = false

        self.estimatedSectionHeaderHeight = 0
        self.sectionHeaderHeight = 0.001

        self.estimatedSectionFooterHeight = 0
        self.sectionFooterHeight = 0.001

        self.estimatedRowHeight = 50
        self.rowHeight = UITableViewAutomaticDimension

        self.backgroundColor = .white
        self.separatorStyle = .none
        self.tableHeaderView = headerView
        self.contentInset = UIEdgeInsets.zero
        self.showsVerticalScrollIndicator = false
        self.showsHorizontalScrollIndicator = false
        self.contentInsetAdjustmentBehavior = .never
        
        if #available(iOS 15.0, *) {
            self.sectionHeaderTopPadding = 0.0
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: - UITableViewCell 基类
class BaseTableViewCell: UITableViewCell {
    /// 子类重写赋值
    var model: BaseTableViewCellModel? {
        didSet {

            guard let model = model else { return }

            topLine.isHidden = !model.showTopLine
            bottomLine.isHidden = !model.showBottomLine
            topLine.backgroundColor = model.lineColor
            bottomLine.backgroundColor = model.lineColor

            topLine.snp.updateConstraints { make in
                make.left.equalTo(model.leftMargin)
                make.right.equalTo(-model.rightMargin)
                make.height.equalTo(model.lineHeight)
            }

            bottomLine.snp.updateConstraints { make in
                make.left.equalTo(model.leftMargin)
                make.right.equalTo(-model.rightMargin)
                make.height.equalTo(model.lineHeight)
            }
        }
    }

    /// 顶部分割线
    private lazy var topLine: UIView = {
        let view = UIView.init(backgroundColor: UIColor.color_eee)
            .pg_isHidden(true)
        return view
    }()

    /// 底部分割线
    private lazy var bottomLine: UIView = {
        let view = UIView.init(backgroundColor: UIColor.color_eee)
            .pg_isHidden(true)
        return view
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupCell()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        contentView.bringSubview(toFront: topLine)
        contentView.bringSubview(toFront: bottomLine)
    }

    private func setupCell() {
        addViews()
        addConstraints()
        self.selectionStyle = .none
        self.backgroundColor = UIColor.color(light: .white, dark: .black)
    }

    private func addViews() {
        contentView.addSubview(topLine)
        contentView.addSubview(bottomLine)
    }

    private func addConstraints() {
        topLine.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(1)
        }

        bottomLine.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(1)
        }
    }
}


